// 滑动窗口最大值
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        multiset<int> hash;
        for (int l = 0, r = 0; r < nums.size(); r++) 
        {
            hash.insert(nums[r]);
            if (r - l + 1 == k)
            {
                res.push_back(*hash.rbegin());
                hash.erase(hash.find(nums[l++]));
            }
        }
        return res;
    }
};
    
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        priority_queue<pair<int, int>> pq;
        for (int i = 0; i < k; i++)
        {
            pq.emplace(nums[i], i);
        }
        res.push_back(pq.top().first);
        for (int i = k; i < nums.size(); i++)
        {
            pq.emplace(nums[i], i);
            while (pq.top().second <= i - k) // 关键 <=
            {
                pq.pop();
            } 
            res.push_back(pq.top().first);
        } 
        return res;
    }
};
        
// 缺失的第一个正数
class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i])
            {
                swap(nums[nums[i] - 1], nums[i]);
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (nums[i] != i + 1)
            {
                return i + 1;
            }
        }
        return n + 1;
    }
};
    
// 动态规划
class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size(); 
        vector<int> f(n, 1), g(n, 1), res(n);
        for (int i = 1; i < n; i++) f[i] = f[i - 1] * nums[i - 1];
        for (int i = n - 2; i >= 0; i--) g[i] = g[i + 1] * nums[i + 1];
        for (int i = 0; i < n; i++) res[i] = f[i] * g[i];
        return res;
    }
};
    